<script>
import {formatTime} from "@/utils/formatTime.js";
import {all, add, edit, del,filter} from "@/api/user.js";

export default {
  name: "user",
  methods: {
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.userData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    addUser(data) {
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addUserDialogVisible = false
          this.addUserFormData = {}
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    handleEdit(data) {
      this.editUserFormData = data;
      this.editUserDialogVisible = true;
    },
    editUser(data) {
      edit(data).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.refresh()
          this.editUserDialogVisible = false
          this.editUserFormData = {}
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    handleDelete(id) {
      del(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    filterUser(data){
      filter(data).then(res => {
        if (res.code === 200) {
          this.userData = res.data
          this.filterDialogVisible = false
        } else {
          this.$message.error("获取数据失败")
        }
      })
    }
  },
  data() {
    return {
      userData: [],
      editUserDialogVisible: false,
      addUserDialogVisible: false,
      filterDialogVisible: false,
      editUserFormData: {},
      addUserFormData: {},
      filterUserFormData: {},
    }
  },
  created() {
    this.refresh();
  }
}
</script>

<template>
  <div id="user-title">
    用户管理
  </div>
  <div id="user-box">
    <div id="user-form">
      <el-button type="primary" @click="filterDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="addUserDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>

    <div>
      <el-table id="user-data" :data="userData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="姓名" prop="name" width="200"></el-table-column>
        <el-table-column label="密码" prop="password" width="200"></el-table-column>
        <el-table-column label="手机号" prop="phone" width="200"></el-table-column>
        <el-table-column label="头像" prop="avatarUrl" width="200">
          <template #default="scope">
            <img :src="scope.row.avatarUrl" style="width: 100px; height: 100px;" alt="头像">
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="400">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
  <!-- 修改表单 -->
  <div>
    <el-dialog title="修改用户信息" v-model="editUserDialogVisible" :close-on-click-modal="false">
      <el-form :model="editUserFormData">
        <el-form-item label="姓名">
          <el-input v-model="editUserFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editUserFormData.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="editUserFormData.password"></el-input>
        </el-form-item>
        <el-form-item label="头像链接">
          <el-input v-model="editUserFormData.avatarUrl"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="editUserDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editUser(editUserFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

  <!-- 添加表单 -->
  <div>
    <el-dialog title="新增用户" v-model="addUserDialogVisible" :close-on-click-modal="false">
      <el-form :model="addUserFormData">
        <el-form-item label="姓名">
          <el-input v-model="addUserFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="addUserFormData.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addUserFormData.password"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="addUserFormData.avatarUrl"></el-input>
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-button type="info" @click="addUserDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="addUser(addUserFormData)">提交</el-button>
          </el-form-item>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <!-- 过滤 -->
  <div>
    <el-dialog title="条件筛选" v-model="filterDialogVisible" :close-on-click-modal="false">
      <el-form :model="filterUserFormData">
        <el-form-item label="用户名">
          <el-input v-model="filterUserFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="filterUserFormData.password"></el-input>
        </el-form-item>  <el-form-item label="手机号">
          <el-input v-model="filterUserFormData.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="filterDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="filterUser(filterUserFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped>
#user-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#user-title {
  font-size: 30px;
  margin-left: 20px;
}

#user-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;

}
</style>